<?php
class Tag
{
	private $_table_tag = 'tags';
	private $_table_tag_fields = array('tag_id', 'tag_name', 'tag_alias');
	
	static protected $_instance = null;
	
	/**
	 * Get object instance
	 */
	
	public static function getInstance()
	{
		if( null === self::$_instance ) {
			self::$_instance = new self();	
		}
		return self::$_instance;
	}
	
	/**
	 * Get tag
	 * @param int $id
	 */
	
	public function getTag($id)
	{
		$storage = My_Globals::getStorage();
		$select = $storage->select()
						->from($this->_table_tag, '*');
		return $storage->fetchRow($select);
	}
	
	/**
	 * Get tag by alias
	 * @param string $alias
	 */
	
	public function getTagByAlias($alias)
	{
		$storage = My_Globals::getStorage();
		$select = $storage->select()
						->from($this->_table_tag, '*')
						->where($storage->quoteInto('tag_alias = ?', $alias));
		return $storage->fetchRow($select);
	}
	
	/**
	 * Delete tag
	 * @param int | array $id
	 */
	
	public function deleteTag($id)
	{
		$storage = My_Globals::getStorage();
		return $storage->delete($this->_table_tag, $storage->quoteInto('tag_id IN (?)', $id));
	}
	
	/**
	 * Save tag
	 * @param array $data
	 */
	
	public function saveTag($data)
	{
		$storage = My_Globals::getStorage();
		$data = My_Globals::initData($this->_table_tag_fields, $data);
		if(isset($data['tag_id']) && $data['tag_id'] > 0) {
			// Update
			$rs = $storage->update($this->_table_tag, $data, 'tag_id = '.$data['tag_id']);
		} else {
			// Insert
			$rs = $storage->insert($this->_table_tag, $data);
		}
		// Clean
		unset($data);
		// Return
		return $rs;
	}
	
	/**
	 * Get tag list
	 * @param int $page
	 * @param int $limit
	 */
	
	public function getTagList($page = null, $limit = null)
	{
		// Set page
		if($page == 0) $page = 1;
		$storage = My_Globals::getStorage();
		$select = $storage->select()
					->from($this->_table_tag, '*')
					->order('tag_name ASC');
		if($limit > 0) {
			$select->limitPage($page, $limit);
		}
		return $storage->fetchAll($select);
	}
	
	/**
	 * Get tag amount
	 */
	
	public function getAmount()
	{
		$storage = My_Globals::getStorage();
		$select = $storage->select()
						->from($this->_table_tag, 'COUNT(*) AS total');
		$row = $storage->fetchRow($select);
		return $row['total'];
	}
	
	/**
	 * Check tag alias exist
	 * @param string $alias
	 */
	
	public function checkTagAliasExist($alias)
	{
		$storage = My_Globals::getStorage();
		$select = $storage->select()
						->from($this->_table_tag, 'COUNT(*) AS total')
						->where($storage->quoteInto('tag_alias = ?', $alias));
		$row = $storage->fetchRow($select);
		if($row['total'] > 0) {
			return true;
		}
		return false;
	}
	
	/**
	 * Get tags of tour
	 * @param int $id
	 */
	public function getTagsOfTour($id)
	{
		$storage = My_Globals::getStorage();
		$select = $storage->select()
						->from($this->_table_tag, '*')
						->join('tours_tags', $this->_table_tag.'.tag_id = tours_tags.tag_id')
						->where('tour_id = ?', $id);
		return $storage->fetchAll($select);
	}
	
	/**
	 * Get tags of topic
	 * @param int $id
	 */
	public function getTagsOfTopic($id)
	{
		$storage = My_Globals::getStorage();
		$select = $storage->select()
						->from($this->_table_tag, '*')
						->join('topics_tags', $this->_table_tag.'.tag_id = topics_tags.tag_id')
						->where('topic_id = ?', $id);
		return $storage->fetchAll($select);
	}
}